"""
业务日志 Request VO
"""

import utils.enum_utils as enum
from models import (
    ListRequestBase,
    QueryFieldBase,
)
from my_typing import *
from utils.validator_utils.pydantic_validator.field import (
    PathCharField,
    QueryCharField,
    QueryIntegerField,
)


class BizLogFilePathField:
    """ 业务日志文件 | 路径 | 字段 """

    file_date: str = PathCharField(..., title="日志文件日期", min_length=10, max_length=10)


class BizLogFileListRequest(ListRequestBase):
    """ 业务日志文件 | 列表 | 请求 """

    # -------------------- 排序字段 --------------------
    sort_by_file_date: enum.MongoSortOrNone = QueryCharField(None, title="根据日志文件日期排序")
    sort_by_file_size: enum.MongoSortOrNone = QueryCharField(None, title="根据日志文件大小排序")

    # -------------------- 查询字段 --------------------
    file_date_end: DateOrNone = QueryCharField(None, title="日志文件结束日期")
    file_date_start: DateOrNone = QueryCharField(None, title="日志文件开始日期")

    @classmethod
    def from_query(
        cls,
        page: int = QueryFieldBase.page,
        page_size: int = QueryFieldBase.page_size,
        sort_by_file_date: enum.MongoSortOrNone = QueryCharField(None, title="根据日志文件日期排序"),
        sort_by_file_size: enum.MongoSortOrNone = QueryCharField(None, title="根据日志文件大小排序"),
        file_date_end: DateOrNone = QueryCharField(None, title="日志文件结束日期"),
        file_date_start: DateOrNone = QueryCharField(None, title="日志文件开始日期"),
    ) -> "BizLogFileListRequest":
        return cls(
            page=page,
            page_size=page_size,
            sort_by_file_date=sort_by_file_date,
            sort_by_file_size=sort_by_file_size,
            file_date_end=file_date_end,
            file_date_start=file_date_start,
        )


class BizLogListRequest(ListRequestBase):
    """ 业务日志 | 列表 | 请求 """

    # -------------------- 排序字段 --------------------
    sort_by_log_level: enum.MongoSortOrNone = QueryCharField(None, title="根据日志级别排序")
    sort_by_log_time: enum.MongoSortOrNone = QueryCharField(None, title="根据日志时间排序")

    # -------------------- 查询字段 --------------------
    code_path: StrOrNone = QueryCharField(None, title="代码路径", max_length=64)
    code_line: IntOrNone = QueryIntegerField(None, title="代码行号", min_value=1, max_value=8192)
    log_level: enum.LoguruLogLevel = QueryCharField(None, title="日志级别")
    log_time_end: DateTimeOrNone = QueryCharField(None, title="日志结束时间")
    log_time_start: DateTimeOrNone = QueryCharField(None, title="日志开始时间")
    message: StrOrNone = QueryCharField(None, title="日志信息", max_length=256)
    process_id: IntOrNone = QueryIntegerField(None, title="进程 ID", min_value=1, max_value=2 ** 32 - 1)
    thread_id: IntOrNone = QueryIntegerField(None, title="线程 ID", min_value=1, max_value=2 ** 32 - 1)

    @classmethod
    def from_query(
        cls,
        sort_by_log_level: enum.MongoSortOrNone = QueryCharField(None, title="根据日志级别排序"),
        sort_by_log_time: enum.MongoSortOrNone = QueryCharField(None, title="根据日志时间排序"),
        code_path: StrOrNone = QueryCharField(None, title="代码路径", max_length=64),
        code_line: IntOrNone = QueryIntegerField(None, title="代码行号", min_value=1, max_value=8192),
        log_level: enum.LoguruLogLevel = QueryCharField(None, title="日志级别"),
        log_time_end: DateTimeOrNone = QueryCharField(None, title="日志结束时间"),
        log_time_start: DateTimeOrNone = QueryCharField(None, title="日志开始时间"),
        message: StrOrNone = QueryCharField(None, title="日志信息", max_length=256),
        process_id: IntOrNone = QueryIntegerField(None, title="进程 ID", min_value=1, max_value=2 ** 32 - 1),
        thread_id: IntOrNone = QueryIntegerField(None, title="线程 ID", min_value=1, max_value=2 ** 32 - 1),
    ) -> "BizLogListRequest":
        return cls(
            sort_by_log_level=sort_by_log_level,
            sort_by_log_time=sort_by_log_time,
            code_path=code_path,
            code_line=code_line,
            log_level=log_level,
            log_time_end=log_time_end,
            log_time_start=log_time_start,
            message=message,
            process_id=process_id,
            thread_id=thread_id,
        )
